package net.jsoft.platform.schedule.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.jsoft.platform.core.web.BaseController;
import net.jsoft.platform.schedule.entity.Schedule;
import net.jsoft.platform.schedule.repository.ScheduleRepository;
import net.jsoft.platform.schedule.util.ScheduleUtils;
import net.sf.json.JSONObject;;

/**
 * 计划管理控制器
 *
 * @author John
 * @Date 2017-08-03 15:51:51
 */
@Controller
@RequestMapping("/schedule")
public class ScheduleController extends BaseController
{

    private String PREFIX = "/platform/schedule/";

    @Resource
    private ScheduleRepository repository;

    //    @Autowired
    //    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 跳转到计划管理首页
     */
    @RequestMapping("/list")
    public String index(Model model)
    {
        return PREFIX + "list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(int pageSize, int pageNumber)
    {
        List<Schedule> list = repository.findAllTriggers();

        List<Schedule> listPage = new ArrayList<Schedule>();

        for (int i = 0; i < list.size(); i++)
        {
            // 获取从上一页开始到当前页结束的 对象
            if (i >= (pageSize * (pageNumber - 1)) && i <= pageNumber * pageSize)
            {
                Schedule record = (Schedule) list.get(i);
                listPage.add(record);
            }
        }

        JSONObject json = new JSONObject();
        json.put("rows", listPage); //这里的 rows 和total 的key 是固定的 
        json.put("total", list.size());
        return json;
    }

    @RequestMapping("/add")
    public String add()
    {
        return PREFIX + "add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Schedule schedule) throws Exception
    {
        ScheduleUtils.addJob(schedule.getJobName(), schedule.getBeanClass(), schedule.getCron(), schedule.getRemark(), schedule.getIp(), schedule.getPort());
        return "ok";
    }

    @RequestMapping("/edit")
    public String edit(String jobName, Model model)
    {
        Schedule schedule = repository.findTriggers(jobName, ScheduleUtils.TRIGGER_GROUP_NAME);
        
        model.addAttribute("schedule", schedule);
        return PREFIX + "edit";
    }

    @RequestMapping("/editSave")
    @ResponseBody
    public String editSave(Schedule schedule)
    {
        try
        {
            ScheduleUtils.jobUpdateSchedule(schedule.getJobName(), schedule.getBeanClass(), schedule.getCron(), schedule.getRemark(), schedule.getIp(), schedule.getPort());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "ok";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String jobName)
    {
        try
        {
            ScheduleUtils.jobdelete(jobName);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //        repository.delete(schedule);
        return "ok";
    }

    @RequestMapping("/reFreshCorn")
    @ResponseBody
    public String reFreshCorn(String jobName, String cronExpression)
    {
        try
        {
            ScheduleUtils.jobreschedule(jobName, cronExpression);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "ok";
    }

    @RequestMapping("/pause")
    @ResponseBody
    public String pause(String jobName) throws Exception
    {
        ScheduleUtils.jobPause(jobName);
        return "ok";
    }

    @RequestMapping("/renew")
    @ResponseBody
    public String renew(String jobName) throws Exception
    {
        ScheduleUtils.jobresume(jobName);
        return "ok";
    }

    @RequestMapping("/doOnce")
    @ResponseBody
    public String doOnce(String jobName) throws Exception
    {
        ScheduleUtils.doOnce(jobName);
        return "ok";
    }

}
